<template>
  <div class="layout-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '220px'" class="sidebar">
      <div class="logo-container">
        <h2 v-if="!isCollapse" class="logo-text">清华美食地图</h2>
        <h2 v-else class="logo-text-mini">清</h2>
      </div>

      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        :unique-opened="true"
        class="sidebar-menu"
        router
      >
        <el-menu-item index="/dashboard">
          <el-icon><HomeFilled /></el-icon>
          <template #title>首页</template>
        </el-menu-item>

        <el-sub-menu index="locations">
          <template #title>
            <el-icon><Location /></el-icon>
            <span>地点管理</span>
          </template>
          <el-menu-item index="/locations">全部地点</el-menu-item>
          <el-menu-item index="/locations/pending">待审核地点</el-menu-item>
        </el-sub-menu>

        <el-menu-item index="/ratings">
          <el-icon><Star /></el-icon>
          <template #title>评分管理</template>
        </el-menu-item>

        <el-menu-item index="/dishes">
          <el-icon><Food /></el-icon>
          <template #title>推荐菜品</template>
        </el-menu-item>

        <el-menu-item index="/users">
          <el-icon><UserFilled /></el-icon>
          <template #title>用户管理</template>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 主内容区 -->
    <el-container class="main-container">
      <!-- 顶部导航栏 -->
      <el-header class="header">
        <div class="header-left">
          <el-icon class="collapse-icon" @click="toggleCollapse">
            <Expand v-if="isCollapse" />
            <Fold v-else />
          </el-icon>
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item v-if="currentRouteMeta">{{
              currentRouteMeta
            }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>

        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <div class="user-info">
              <el-avatar :size="32" :icon="UserFilled" />
              <span class="username">{{ userStore.userInfo?.username || '管理员' }}</span>
              <el-icon><ArrowDown /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>
                  个人信息
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 主内容 -->
      <el-main class="main-content">
        <router-view v-slot="{ Component }">
          <transition name="fade-slide" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
import {
  HomeFilled,
  Location,
  Star,
  Food,
  UserFilled,
  Expand,
  Fold,
  ArrowDown,
  User,
  SwitchButton
} from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const isCollapse = ref(false)

const activeMenu = computed(() => route.path)
const currentRouteMeta = computed(() => route.meta.title as string)

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const handleCommand = (command: string) => {
  if (command === 'logout') {
    ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      userStore.logout()
      ElMessage.success('已退出登录')
      router.push('/login')
    })
  } else if (command === 'profile') {
    ElMessage.info('个人信息功能开发中...')
  }
}

// 组件挂载时获取用户信息
onMounted(async () => {
  if (userStore.token && !userStore.userInfo) {
    try {
      await userStore.getUserInfo()
    } catch (error) {
      console.error('获取用户信息失败:', error)
    }
  }
})
</script>

<style scoped>
.layout-container {
  display: flex;
  height: 100vh;
  background: #f0f2f5;
}

.sidebar {
  background: linear-gradient(180deg, #1a1d2e 0%, #0f1117 100%);
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  transition: width 0.3s;
  overflow-x: hidden;
}

.logo-container {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.logo-text {
  font-size: 20px;
  font-weight: 700;
  color: #fff;
  margin: 0;
  white-space: nowrap;
  letter-spacing: 2px;
  text-shadow: 0 0 10px rgba(66, 153, 225, 0.5);
}

.logo-text-mini {
  font-size: 24px;
  font-weight: 700;
  color: #fff;
  margin: 0;
}

.sidebar-menu {
  border: none;
  background: transparent;
}

.sidebar-menu :deep(.el-menu-item) {
  color: rgba(255, 255, 255, 0.7) !important;
  background: transparent !important;
  transition: all 0.3s;
}

.sidebar-menu :deep(.el-sub-menu__title) {
  color: rgba(255, 255, 255, 0.7) !important;
  background: transparent !important;
  transition: all 0.3s;
}

.sidebar-menu :deep(.el-menu-item:hover),
.sidebar-menu :deep(.el-sub-menu__title:hover) {
  background: rgba(66, 153, 225, 0.1) !important;
  color: #4299e1;
}

.sidebar-menu :deep(.el-menu-item.is-active) {
  background: linear-gradient(90deg, rgba(66, 153, 225, 0.2) 0%, transparent 100%) !important;
  color: #4299e1 !important;
  border-right: 3px solid #4299e1;
}

/* 菜单图标样式 */
.sidebar-menu :deep(.el-icon) {
  color: inherit;
}

/* 展开的子菜单容器 */
.sidebar-menu :deep(.el-sub-menu .el-menu) {
  background: rgba(0, 0, 0, 0.15) !important;
}

/* 二级菜单项样式 */
.sidebar-menu :deep(.el-menu--inline .el-menu-item) {
  color: rgba(255, 255, 255, 0.65) !important;
  background: transparent !important;
  padding-left: 50px !important;
  min-width: 0;
  margin: 2px 8px;
  border-radius: 6px;
  transition: all 0.3s;
}

.sidebar-menu :deep(.el-menu--inline .el-menu-item:hover) {
  background: rgba(66, 153, 225, 0.2) !important;
  color: #64b5f6 !important;
}

.sidebar-menu :deep(.el-menu--inline .el-menu-item.is-active) {
  background: linear-gradient(90deg, rgba(66, 153, 225, 0.3) 0%, rgba(66, 153, 225, 0.1) 100%) !important;
  color: #64b5f6 !important;
  font-weight: 500;
}

/* 折叠状态下的提示文字 */
.sidebar-menu :deep(.el-tooltip__trigger) {
  color: rgba(255, 255, 255, 0.7) !important;
}

.main-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.header {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.collapse-icon {
  font-size: 20px;
  cursor: pointer;
  color: #606266;
  transition: color 0.3s;
}

.collapse-icon:hover {
  color: #4299e1;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 8px;
  transition: background 0.3s;
}

.user-info:hover {
  background: #f5f7fa;
}

.username {
  font-size: 14px;
  color: #303133;
  font-weight: 500;
}

.main-content {
  background: #f0f2f5;
  padding: 20px;
  overflow-y: auto;
}

/* 页面切换动画 */
.fade-slide-enter-active,
.fade-slide-leave-active {
  transition: all 0.3s ease;
}

.fade-slide-enter-from {
  opacity: 0;
  transform: translateX(20px);
}

.fade-slide-leave-to {
  opacity: 0;
  transform: translateX(-20px);
}
</style>
